﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
using log4net.Repository;

namespace 综合同步
{
    enum LogType
    {
        Info,
        Warning,
        Error
    }
    public class FileLogOperator
    {
        public static readonly log4net.ILog mlog;
        public static bool m_bSaveLog = LocalServer.GetConfigValue<bool>("logs");

        static FileLogOperator()
        {
            ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository");
            XmlConfigurator.Configure(repository, new FileInfo("log4net.xml"));
            mlog = LogManager.GetLogger(repository.Name,"NETCorelog4net");
        }
        public static void Info(object info,ConsoleColor color=ConsoleColor.Gray)
        {
            try{
                lock(Console.Out){
                    Console.ForegroundColor = color;
                    Console.Out.WriteLine("【"+DateTime.Now.ToString("G")+"】 "+info);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                if (mlog.IsInfoEnabled && m_bSaveLog)
                    mlog.Info(info);
            }
            catch { }
        }
        public static void Debug(object debug,ConsoleColor color=ConsoleColor.Gray)
        { 
            try{
                lock(Console.Out){
                    Console.ForegroundColor = color;
                    Console.Out.WriteLine("【"+DateTime.Now.ToString("G")+"】 "+debug);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                if (mlog.IsDebugEnabled && m_bSaveLog)
                    mlog.Debug(debug);
            }catch{}
        }
        public static void Fatal(object fatal,ConsoleColor color=ConsoleColor.Gray)
        {
            try{
                lock(Console.Out){
                    Console.ForegroundColor = color;
                    Console.Out.WriteLine("【"+DateTime.Now.ToString("G")+"】 "+fatal);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                if (mlog.IsFatalEnabled && m_bSaveLog)
                    mlog.Fatal(fatal);
            }
            catch { }
        }
        public static void Error(object error,ConsoleColor color=ConsoleColor.Gray)
        {
            try{
                lock(Console.Out){
                    Console.ForegroundColor = color;
                    Console.Out.WriteLine("【"+DateTime.Now.ToString("G")+"】 "+error);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                if (mlog.IsErrorEnabled && m_bSaveLog)
                    mlog.Error(error);
            }
            catch { }
        }
        public static void Warn(object warning,ConsoleColor color=ConsoleColor.Gray)
        {
            try{
                lock(Console.Out){
                    Console.ForegroundColor = color;
                    Console.Out.WriteLine("【"+DateTime.Now.ToString("G")+"】 "+warning);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                if (mlog.IsWarnEnabled && m_bSaveLog)
                    mlog.Warn(warning);
            }
            catch { }
        }
    }
}
